package com.jbm.jbmsupplier.net;

import android.os.Bundle;
import android.os.Handler;

import com.jbm.core.iface.IAcross;
import com.jbm.onehome.common.OHInterface;
import com.jbm.onehome.iface.IAplipay;
import com.jbm.onehome.iface.IAppVersion;
import com.jbm.onehome.iface.IJOrder;
import com.jbm.onehome.iface.IJOrderCtrl;
import com.jbm.onehome.iface.IOrder;
import com.jbm.onehome.iface.IOrderCtrl;
import com.jbm.onehome.iface.IPayCtrl;
import com.jbm.onehome.iface.ISeller;
import com.jbm.onehome.iface.IorderEntity;
import com.jbm.onehome.iface.JobOrderEntity;
import com.jbm.onehome.iface.Media;

import java.math.BigDecimal;
import java.util.Calendar;


/**
 * Created by 家帮忙 on 2017/4/10.
 */

public class NetIntent /*extends SendMessageInterfase */{
    private final String CMD_CODE="code";
    private final String DETECTION="检测版本号";
    private final String LOGIN="登录";
    private final String ORDERDQR="待确认";
    private final String HQMONEY="押金总额";
    private final String PLEDGE="押金详细";//修改基金流水
    private final String INCOME="收入明细";
    private final String XIUGAIMIMA="修改密码";
    private final String HUOQUBAOXIUJINE="获取报修金额";
    private final String CONFIRM="确定订单";
    private final String ACCOM="完成订单";
    private final String UPMEDIA="上传图片";
    private final String TERMINOR="终止订单";
    private final String LOADMEDIA="下载图片";
//    private final String LOADMEDIT="下载图片S";
    private final String PAYOFFER="线下支付";
    private final String PAYDEMO="支付宝支付";
    private final String EXITLOGIN="退出登录";
    private Bundle mbundle;
    private Handler handler;
    private Media media;
private   OHInterface Ohinterface;

    public NetIntent(Handler handler) {
        this.handler = handler;
         Ohinterface = OHInterface.getG_server();
    }


    protected void RunContext() {
        try {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String code= mbundle.getString(CMD_CODE);

                switch (code){
                    case "检测版本号":
                        IAppVersion vidion = Ohinterface.GetLastVersion("jbmseller.android",mbundle.getInt("max"),mbundle.getInt("min"),0,0);
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("hvision"),vidion));
                        break;
                    case "登录":
                        String logins = Ohinterface.Login(mbundle.getString("name"),mbundle.getString("pwd"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("login"),logins));
                        break;
                    case "待确认":
                        IJOrderCtrl listorder = Ohinterface.GetJOrderListN(mbundle.getString("stati"), mbundle.getInt("star"), mbundle.getInt("end"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("dqr"),listorder));
                        break;
                    case "押金总额":
                        ISeller ymoney = Ohinterface.GetMyselfSeller();
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("money"),ymoney));
                        break;
                    case "押金详细":
//                        IPayCtrl pled = Ohinterface.GetMyPay("", 0, 0, "", "");
                        IPayCtrl pled = OHInterface.getG_server().getPayListS(mbundle.getInt("page"),10);
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("pled"),pled));
                        break;
                    case "收入明细":
                        Calendar calendar = Calendar.getInstance();
                        String startDate = calendar.get(Calendar.YEAR) + ":"
                                + (calendar.get(Calendar.MONTH)) + ":"//从0计算
                                + calendar.get(Calendar.DAY_OF_MONTH);

                        String endDate = calendar.get(Calendar.YEAR) + ":"
                                + (calendar.get(Calendar.MONTH)+1) + ":"//从0计算
                                + calendar.get(Calendar.DAY_OF_MONTH);
                        IOrderCtrl ctrl = Ohinterface.GetMyOrderList("'comment','completed'",startDate,endDate,0,0);
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("income"),ctrl));
                        break;
                    case "修改密码":
                        IAcross ret = Ohinterface.ChangePass(mbundle.getString("pwd"), mbundle.getString("pwds"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("ament"),ret));
                        break;
                    case "获取报修金额":
                        IOrder oramun = Ohinterface.LoadOrder(mbundle.getInt("orid"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("amout"),oramun));
                        break;
                    case "确定订单":
                        IJOrder confir = Ohinterface.ConfirmJob(mbundle.getInt("orid"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("confirm"),confir));
                        break;
                    case "完成订单":
                            IJOrder sub = Ohinterface.SubmitJob(mbundle.getInt("orid"), mbundle.getString("jine"),mbundle.getString("ms"),mbundle.getString("riqi"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("accom"),sub));
                        break;
                    case "上传图片":
                        Media me = Ohinterface.UploadMedia(mbundle.getInt("jid"),"job_order",media);
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("upmed"),me));
                        break;
                    case"终止订单":
                        IJOrder bre = Ohinterface.BreakJob(mbundle.getInt("jid"), mbundle.getString("neirong"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("Termin"),bre));
                        break;
                    case "下载图片":
                        JobOrderEntity loadme = Ohinterface.LoadJOrderN(mbundle.getInt("jid"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("pic"),loadme));
                        break;
                    case "线下支付":
                        IAcross payoff = Ohinterface.payOffLine(mbundle.getInt("orid"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("payoff"),payoff));
                        break;
                    case "下载图片S":
                        IorderEntity orderinfo = Ohinterface.LoadOrderInfo(mbundle.getInt("jid"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("pic"),orderinfo));
                        break;
                    case "支付宝支付":
                        IAplipay paydemo = Ohinterface.getAplipayRehargeS(new BigDecimal(mbundle.getDouble("amout")), mbundle.getString("payname"));
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("payde"),paydemo));
                        break;
                    case "退出登录":
                        IAcross exit = OHInterface.getG_server().Logout();
                        if(handler!=null){
                        handler.sendMessage(handler.obtainMessage(mbundle.getInt("ext"),exit));}
                        break;

                }
            }
        }).start();
        }catch (Exception e){
            com.example.weather.myframe.Log.e("main",e);
        }
    }
    /**
     * 退出登录
     */
    public void setExit(int ext){
        this.mbundle=new Bundle();
        this.mbundle.putString(CMD_CODE,EXITLOGIN);
        this.mbundle.putInt("ext",ext);
        RunContext();

    }

    /**
     * 2017年4月10日11:17:49
     * 检测版本号
     */
public void setDetection(int max,int min,int hvision){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,DETECTION);
    mbundle.putInt("max",max);
    mbundle.putInt("min",min);
    mbundle.putInt("hvision",hvision);
   // this.Run();
    this.RunContext();

}
    /**
     * 2017年4月10日11:17:49
     * 登录
     */
public void setLogin(String name,String pwd,int login){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,LOGIN);
    mbundle.putString("name",name);
    mbundle.putString("pwd",pwd);
    mbundle.putInt("login",login);
   // this.Run();
    this.RunContext();
}
/**
 * 2017年4月10日16:33:32
 * 获取待确认订单
 */
public void setOrderDqr(String stati,int star,int end,int dqr){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,ORDERDQR);
    mbundle.putString("stati",stati);
    mbundle.putInt("star",star);
    mbundle.putInt("end",end);
    mbundle.putInt("dqr",dqr);
    //this.Run();
    this.RunContext();
}
/**
 * 2017年4月10日17:58:40
 * 获取押金总额
 */
public void setMoney(int money){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,HQMONEY);
    mbundle.putInt("money",money);
   // this.Run();
    this.RunContext();
}
/**
 * 2017年4月11日11:15:01
 * 获取押金详细订单
 */
public void setPled(int pled){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,PLEDGE);
    mbundle.putInt("pled",pled);
   // this.Run();
    this.RunContext();
}

    /**
     * 2017年4月11日11:15:01
     * 基金流水
     */
    public void getPayListS(int pled,int page){
        mbundle=new Bundle();
        mbundle.putString(CMD_CODE,PLEDGE);
        mbundle.putInt("page",page);
        mbundle.putInt("pled",pled);
        // this.Run();
        this.RunContext();
    }




    /**
     * 2017年4月11日16:16:11
     * 收入金额
     */
public void setIncoment(int income){

    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,INCOME);
    mbundle.putInt("income",income);
    //this.Run();
    this.RunContext();

}
    /**
     * 2017年4月11日16:51:27
     * 修改密码
     */
    public void setAment(String pws,String pwds,int ament){
        mbundle=new Bundle();
        mbundle.putString(CMD_CODE,XIUGAIMIMA);
        mbundle.putString("pwd",pws);
        mbundle.putString("pwds",pwds);
        mbundle.putInt("ament",ament);
       // this.Run();
        this.RunContext();
    }
    /**
     * 2017年4月13日13:43:39
     * 获取维修金额
     */
    public void setAmount(int orid,int amout){
        mbundle=new Bundle();
        mbundle.putString(CMD_CODE,HUOQUBAOXIUJINE);
        mbundle.putInt("orid",orid);
        mbundle.putInt("amout",amout);
      //  this.Run();
        this.RunContext();
    }

    /**
     * 2017年4月14日09:49:58
     * 确认订单
     */
public void setConfirm(int orid,int confirm){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,CONFIRM);
    mbundle.putInt("orid",orid);
    mbundle.putInt("confirm",confirm);
   // this.Run();
    this.RunContext();
}
/**
 * 2017年4月14日11:02:28
 * 完成订单
 */

 public void setAccomPlis(int orid,String riqi,String ms,String jine,int accom){
     mbundle=new Bundle();
     mbundle.putString(CMD_CODE,ACCOM);
     mbundle.putInt("orid",orid);
     mbundle.putString("riqi",riqi);
     mbundle.putString("ms",ms);
     mbundle.putString("jine",jine);
     mbundle.putInt("accom",accom);
    // this.Run();
     this.RunContext();
    }
/**
 * 2017年4月14日11:32:31
 * 上传施工段完成订单的照片
 */
   public void setUpMedia(int jid, Media media, int upmed){
       mbundle=new Bundle();
       mbundle.putString(CMD_CODE,UPMEDIA);
       this.media=media;
       mbundle.putInt("jid",jid);
       mbundle.putInt("upmed",upmed);
      // this.Run();
       this.RunContext();
   }
/**
 * 2017年4月14日15:09:38
 * 终止订单
 */
public void setTerminOr(int jid,String neirong,int Termin){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,TERMINOR);
    mbundle.putInt("jid",jid);
    mbundle.putString("neirong",neirong);
    mbundle.putInt("Termin",Termin);
    //this.Run();
    this.RunContext();
}

    /**
     * 2017年4月18日13:32:59
     * 工单下载图片
     */
    public void setLoadPic(int jid,int pic){
        mbundle=new Bundle();
        mbundle.putString(CMD_CODE,LOADMEDIA);
        mbundle.putInt("jid",jid);
        mbundle.putInt("pic",pic);
     //   this.Run();
        this.RunContext();
    }
    /**
     * 2017年4月22日10:32:07
     * 订单获取图片
     */

//    public void setLoadPics(int jid,int pic){
//        mbundle=new Bundle();
//        mbundle.putString(CMD_CODE,LOADMEDIT);
//        mbundle.putInt("jid",jid);
//        mbundle.putInt("pic",pic);
//        this.Run();
//    }




    /**
 * 2017年4月21日13:38:50
 * 线下支付
 */
public void payOffLine(int orid,int payoff){
    mbundle=new Bundle();
    mbundle.putString(CMD_CODE,PAYOFFER);
    mbundle.putInt("orid",orid);
    mbundle.putInt("payoff",payoff);
   // this.Run();
    this.RunContext();
}

    /**
     * 支付宝支付
     * 2017年5月25日 16:42:29
     */
    public void getAplipayRehargeC(int payde,double amout,String payname){
        mbundle=new Bundle();
        mbundle.putString(CMD_CODE,PAYDEMO);
        mbundle.putInt("payde",payde);
        mbundle.putDouble("amout",amout);
        mbundle.putString("payname",payname);
        // this.Run();
        this.RunContext();
    }



}
